package com.Servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * Servlet implementation class salesModel
 */
@WebServlet("/salesModel")
public class salesModel extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public salesModel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/octet-stream");
	    response.setCharacterEncoding("UTF-8");
	    response.addHeader("Content-Disposition", "attachment; filename=salesModel.xls"); 

		
		
		String headline="销售信息";
		String[] title= {"学生编号","中文名","英文名","性别","出生日期","预约记录(已预约或未预约)","是否到访(已出席或未出席)","信息来源","市场推广员","课程顾问","电话课程顾问","分配状态","分配时间","联系电话1","联系电话2","备注","录入人员"};
		//创建EXCEL工作簿
			HSSFWorkbook workbook = new HSSFWorkbook();
			//创建一个工作sheet
			HSSFSheet sheet = workbook.createSheet();
			CellStyle cellStyle = workbook.createCellStyle();
			DataFormat format = workbook.createDataFormat();  
			cellStyle.setDataFormat(format.getFormat("@"));//默认为文本格式
			//创建第一行
			HSSFRow row = sheet.createRow(0);
			HSSFCell cell = null ;
			//创建标题
			sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
			cell =row.createCell(0);
			cell.setCellValue(headline);
			//创建第二行
			HSSFRow rowtitle = sheet.createRow(1);
			//插入第二行title 
			for(int i=0;i<title.length;i++) {
			sheet.setDefaultColumnStyle(i, cellStyle);
			sheet.setColumnWidth(i, 20* 256);//设置好cell的宽度
			cell =rowtitle.createCell(i);
			cell.setCellValue(title[i]);
			}
			//追加数据,1先从数据库中获取结果集并且放到LIST中，然后一行一行地放入
			
			
			//创建一个文件
			workbook.write(response.getOutputStream());
	}

}
